package com.generator.storageManage.storeBrowsing.service.impl;

import com.generator.storageManage.storeBrowsing.pojo.City;
import com.generator.storageManage.storeBrowsing.pojo.Province;
import com.generator.storageManage.storeBrowsing.pojo.OutCountObject;
import com.generator.storageManage.storeBrowsing.dao.IStoreBrowsing;
import com.generator.storageManage.storeBrowsing.pojo.User;
import com.generator.storageManage.storeBrowsing.service.IStoreBrowsingService;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StoreBrowsingService implements IStoreBrowsingService {

    @Autowired
    private IStoreBrowsing storeManage;

    public IStoreBrowsing getStoreManage() {
        return storeManage;
    }

    public void setStoreManage(IStoreBrowsing storeManage) {
        this.storeManage = storeManage;
    }

    //拿取仓库（指定条件）
    @Override
    public List<OutCountObject> getTotalOutCount(OutCountObject outCountDetail) {
        List<OutCountObject> warehouseList = storeManage.getWarehouseList(outCountDetail);
        for (OutCountObject outCountObject : warehouseList) {
            switch (outCountObject.getStateid()){
                case "1":outCountObject.setState("可用");break;
                case "2":outCountObject.setState("不可用");break;
                default:outCountObject.setState("？？？");
            }
        }
        return warehouseList;
    }

    //分页
    public PageModel<OutCountObject> getTotalOutCountPage(OutCountObject outCountDetail, PageModel<OutCountObject> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<OutCountObject> totalOutCount = getTotalOutCount(outCountDetail);
        PageInfo<OutCountObject> warehousePageInfo = new PageInfo<OutCountObject>(totalOutCount);

        pageModel.setData(new ArrayList<OutCountObject>(totalOutCount));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        return pageModel;
    }



    //拿取一个仓库
    @Override
    public OutCountObject getOneWareHouse(Integer id) {
        OutCountObject outCountObject = new OutCountObject();
        outCountObject.setId(id);
        List<OutCountObject> warehouseList = storeManage.getWarehouseList(outCountObject);
        return warehouseList.get(0);
    }

    //拿取所有省份
    @Override
    public List<Province> getAllProvince(Province province) {
        return storeManage.getAllProvince(province);
    }

    //根据省份拿取所有市区
    @Override
    public List<City> getAllCity(City city) {
        return storeManage.getAllCity(city);
    }

    /**
     * 查找用户（仓管）
     */
    @Override
    public List<User> getUserList(User user) {
        return storeManage.getUserList(user);
    }

    /**
     * 更新仓库信息
     * @param outCountObject
     * @return
     */
    @Override
    public Integer storageUpdate(OutCountObject outCountObject) {

        Province province = new Province();
        City city = new City();
        User user = new User();
        List<Province> allProvince = storeManage.getAllProvince(province);
        List<City> allCity = storeManage.getAllCity(city);
        List<User> userList = storeManage.getUserList(user);
        for (City city1 : allCity) {
            if(city1.getCname().equals(outCountObject.getHouseaddresscity())){
                outCountObject.setHouseaddresscityid(city1.getCid());
            }
        }
        for (Province province1 : allProvince) {
            if(province1.getPname().equals(outCountObject.getHouseaddress())){
                outCountObject.setHouseaddressid(province1.getPid());
            }
        }
        for (User user1 : userList) {
            if(user1.getName().equals(outCountObject.getPrinciple())){
                outCountObject.setPrincipleid(user1.getUserid());
            }
        }

        return storeManage.storageUpdate(outCountObject);
    }

    /**
     * 创建仓库
     * @param outCountObject
     * @return
     */
    @Override
    public Integer addStorage(OutCountObject outCountObject) {

        return storeManage.addStorage(outCountObject);
    }
}
